<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>编辑发送信息</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
	<link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>
	<link rel="stylesheet" href="${ctx}/systemStatic/style/admin.css">
	<link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>
	<link rel="stylesheet" href="${ctx}/css/public.css">
	<script type="text/javascript">
        var __ctx = "${ctx}";
	</script>
</head>
<body>

<div>
<div class="layui-fluid "style="padding: 10px;position: absolute;left: 0;overflow: auto;top: 0; bottom: 0;right: 0;">
	<div class="layui-row">
		<div class="layui-col-xs12">
			<div class="layui-card">
				<div class="layui-card-header">
					<a href="#/system/messageSend/view/form" title="返回">
						<i class="layui-icon layui-icon-return"></i>
					</a>
					<span class="indexColumnTitle">编辑消息</span>
				</div>
				<div class="layui-card-body">

<form class="layui-form editViewForm"   lay-filter="editViewForm" style="padding:20px 28px 20px 0;background: #fff" >
	<div class="layui-form-item">
		<label class="layui-form-label">标题</label>
		<div class="layui-input-block" style="width:45%">
			<input type="text" name="subject" lay-verify="subject" autocomplete="off"  class="layui-input">
	</div>
	</div>

	<div class="layui-form-item">

		<label class="layui-form-label">收信人:</label>
		<div class="layui-input-block" style="width: 90%" >
			<div  style="width:50%;display: inline-block">
				<input   readonly="readonly" type="text" name="receiverName"
						 lay-verify="receiverName" autocomplete="off" class="layui-input">
			</div>
			<button class="layui-btn selectUser" inputIndex="1" type="button">选择</button>
			<button class="layui-btn cleanUser" type="button">清空</button>
		</div>
	</div>




	<div class="layui-form-item">

		<label class="layui-form-label">收信组织:</label>
		<div class="layui-input-block" style="width: 90%">
			<div style="width:50%;display: inline-block">
				<input type="text"  readonly="readonly"
					   name="receiverOrgName" lay-verify="receiverOrgName" autocomplete="off" class="layui-input">
			</div>
			<button class="layui-btn selectOrg" inputIndex="1" type="button">选择</button>
			<button class="layui-btn cleanOrg" type="button">清空</button>

		</div>
	</div>

	<!--<div class="layui-form-item">
		<label class="layui-form-label">附件</label>
		<div class="layui-input-block" style="width:90%">
			<input type="text" name="attachment" lay-verify="attachment" autocomplete="off"  class="layui-input">
		</div>
	</div>-->

	<div class="layui-form-item">
		<label class="layui-form-label">需要回复：</label>
		<div class="layui-input-block" >
			<input type="radio" name="canReply" value="1"  checked="" title="是">
			<input type="radio" name="canReply" value="0"  title="否" >
		</div>
	</div>
			<div class="layui-form-item">
				<label class="layui-form-label">内容:</label>
				<div class="layui-input-block" style="width:90%">
					<textarea style="height:300px" id="content"  name="content" id="messageUrl" style="display:none" class="layui-textarea"></textarea>
				</div>
			</div>
	<div class="layui-form-item" style="text-align: center">
		<div class="layui-input-block" style="text-align: center;margin: 0">
			<button  class="layui-btn add" lay-submit lay-filter="add"   type="submit">发送</button>
			<a class="layui-btn layui-btn-danger return cancelAddedit"title="返回">
				返回
			</a>
		</div>
		<input type="hidden" name="id"  />
		<input type="hidden" name="inContent" />
		<input type="hidden" name="messageType" value="1"/>
		<input type="hidden" name="receiverId">
		<input type="hidden" name="receiverOrgId">
	</div>
</form>
				</div>
			</div>
		</div>
	</div>
</div>
</div>
<script src="${ctx}/public/component.js" type="text/javascript"></script>
<script src="${ctx}/layui/layui.js"></script>
<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','layer','commons','setter','form','layedit'], function() {
        var layer = layui.layer,commons=layui.commons,setter=layui.setter,form=layui.form;var layedit = layui.layedit;
        var messageId = '${messageId}';
        var index= layedit.build('content',{
            tool: ['strong' //加粗
                ,'italic' //斜体
                ,'underline' //下划线
                ,'del' //删除线
                ,'|' //分割线
                ,'left' //左对齐
                ,'center' //居中对齐
                ,'right' //右对齐
                ,'face' //表情
            ]
        });

        $(".cancelAddedit").click(function () {
            $(this).attr("href", serverPath.systemPath+setter.sysLinkPath+"messageSend/view/form");
        });

        //表单初始化
        $.ajax({
            url: serverPath.systemPath + setter.sysLinkPath + "messageSend/edit",
            type: "get",
            contentType: 'application/json',
            dataType: 'json',
            data: {messageId: messageId},
            success: function (data) {
                if (commons._filterRecord(data)) {
                    var returnDate = data.data;
                    form.val('editViewForm',{
                        "subject": returnDate.subject,
                        "receiverName": returnDate.receiverName,
                        "receiverOrgName": returnDate.receiverOrgName,
                      /*  "attachment": returnDate.attachment,*/
                        "canReply": returnDate.canReply === 1 ? '是' : '否',
                        "inContent": returnDate.content,
						"id":returnDate.id
                    });
                    var body=$("input[name='inContent']").val();
                    layedit.setContent(index,body);
                } else {
                    window.parent.layer.alert(data.msg);
                }
            }
        });
        //表单提交验证
        form.verify({
            subject: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (value == null || value == "") {
                    return '标题不能为空';
                }

            },
            receiverName: function (value, item) { //value：表单的值、item：表单的DOM对象
                if ((value == null || value == "")&&($("input[name='receiverOrgName']").val()==null||$("input[name='receiverOrgName']").val()=="")) {
                    return '收信人或收信组织不能为空';
                }
            }
        });
        //监听表单提交
        form.on('submit(add)', function (data) {
            data.field.content = layedit.getContent(index);
            $.ajax({
                url: serverPath.systemPath + setter.sysLinkPath + "messageSend/add",
                type: "POST",
                contentType: 'application/json',
                dataType: 'json',
                data: JSON.stringify(data.field),
                success: function (data) {
                    if (commons._filterRecord(data)) {
                      layer.alert(data.msg,function(index) {
                          layer.close(index);
                          window.location.href ='#/system/messageSend/view/form' ;
                        });
                    } else {
                        window.parent.layer.alert(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert("保存失败！" + XMLHttpRequest.status);
                },
            });
				return false;
        });
        //清空组织选择
        $(".cleanOrg").click(function () {
            $("input[name='receiverOrgName']").attr("value", '');
            $("input[name='receierOrgId']").attr("value", '');
        })

        $(".cleanUser").click(function () {
            $("input[name='receiverName']").prop("value", '');
            $("input[name='receiverId']").prop("value", '');
        })
        $(".cancelView").click(function () {
            window.parent.location.href = window.parent.location.pathname + '#'+layui.setter.sysLinkPath+'messageSend/view/form' ;
        })
        layui.use(['layer','setter'], function () {
            var setter = layui.setter
                ,layer = layui.layer;
            $('.selectUser').click(function () {
                var userSelectorWin;
                layer.open({
                    type:2
                    ,title:'人员选择器'
                    ,area:['980px','630px']
                    ,resize:false
                    ,content: serverPath.systemPath + setter.sysLinkPath+'dialog/view/sysUserDialog'
                    ,success: function(layero, index){
                        userSelectorWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    }
                    ,end:function(){
                        dateReturn(userSelectorWin.layui.userList);
                    }
                });
            })
        });
        layui.use(['layer', 'setter'], function () {
            var setter = layui.setter
                , layer = layui.layer;
            $('.selectOrg').click(function () {
                var orgSelectorWin;
                layer.open({
                    type: 2
                    , title: '组织选择器'
                    , area: ['980px', '630px']
                    , resize: false
                    , content: serverPath.systemPath + setter.sysLinkPath+'dialog/view/sysOrgDialog'
                    , success: function (layero, index) {
                        orgSelectorWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    }
                    , end: function () {

                        orgDateReturn(orgSelectorWin.layui.orgList);
                    }
                });
            })
        });
        //组织选择器值回填
        function orgDateReturn(obj) {
            var receiverOrgName = '';
            var receiverOrgId = '';
            for (var c in obj) {
                if (c == 0) {
                    receiverOrgId = obj[c].orgId;
                    receiverOrgName = obj[c].name;
                } else {
                    receiverOrgName = receiverOrgName + ',' + obj[c].name;
                    receiverOrgId=  receiverOrgId + ',' + obj[c].orgId;
                }
            }

            $("input[name='receiverOrgName']").prop("value", receiverOrgName);
            $("input[name='receiverOrgId']").prop("value", receiverOrgId);
        }
        //人员选择器回填
        function dateReturn(obj) {
            var receiverName= '';
            var receierId='';
            for (var c in obj)
            {
                if(c==0){

                    receierId=obj[c].userId;
                    receiverName=obj[c].name;
                }
                else {
                    receiverName=receiverName+','+obj[c].name;
                    receierId=receierId+','+obj[c].userId;
                }
            }
            $("input[name='receiverName']").prop("value",receiverName);
            $("input[name='receiverId']").prop("value",receierId);
        }

    })
</script>
</body>
</html>